package com.qiche.controller.mgr;

import com.github.pagehelper.PageInfo;
import com.qiche.dto.GiftPackageDto;
import com.qiche.dto.OwnerWelfareDto;
import com.qiche.dto.ProductDto;
import com.qiche.model.*;
import com.qiche.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 礼包管理控制器
 * Created by Linzw on 2018/01/24.
 */
@Api(value = "/giftPackages",description = "礼包管理控制器")
@RestController
@RequestMapping("/giftPackages")
public class GiftPackageController {
	@Resource
	private MerchantService merchantService;
	@Resource
	private GiftPackageService gpService;
	@Resource
	private CardLinkService linkService;
	@Resource
	private ProductService productService;
	@Resource
	private InsurancePolicyService policyService;
	@Autowired
	private StoreMemberService storeMemberService;

	@ApiOperation(value = "根据条件和分页获取礼包列表")
	@GetMapping
	public Map<String, Object> getAll(GiftPackage pack,
			@RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int limit) {
		Map<String, Object> map = new HashMap<>();
		User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
		if (currentUser.getMerchantId() != 0 && currentUser.getMerchantId() != null) {
			pack.setMerchantId(currentUser.getMerchantId().longValue());
		}
		PageInfo<GiftPackage> pageInfo = gpService.selectByPage(pack, page, limit);
		List<GiftPackageDto> dtoList = new ArrayList<>();
		for (GiftPackage packs : pageInfo.getList()) {
			InsurancePolicy policy = null;
			if (packs.getPolicyId() > 0) {
				policy = policyService.selectById(packs.getPolicyId());
			}
			GiftPackageDto dto = new GiftPackageDto();
			dto.setId(packs.getId());
			dto.setCode(packs.getCode());
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			if (policy != null) {
				dto.setCarInfo(policy.getCarInfo());
				dto.setPolicyExpiryTime(formatter.format(policy.getExpiryTime()));
			} else {
				dto.setPolicyExpiryTime("无");
			}
			dto.setPolicyCode(packs.getPolicyCode());
			dto.setPlateNumber(packs.getPlateNumber());
			dto.setPhone(packs.getPhone());
			dto.setStatus(packs.getStatus().toString());
			dto.setDefine1(packs.getId() + "_" + packs.getStatus().toString());

			Merchant merchant=merchantService.selectById(packs.getMerchantId());
			dto.setBelongMerchantId(packs.getMerchantId());
			if(merchant!=null){

				dto.setBelongMerchantName(merchant.getName());
			}
			dtoList.add(dto);

		}
		map.put("code", 0);
		map.put("msg", "查询成功");
		map.put("count", pageInfo.getTotal());
		map.put("data", dtoList);
		return map;
	}

	@ApiOperation(value = "根据礼包ID获取礼包信息")
	@PostMapping(value = "/getById")
	public Map<String, Object> getById(@RequestParam("id") Long packId) throws Exception{
		GiftPackage gp=gpService.selectById(packId);
		List<CardLink> links=null;
		if(gp!= null) {
			links=	linkService.selectByPack(gp.getId());
		}
		StoreMember storeMember =storeMemberService.selectByKey(gp.getOwnerId());
		InsurancePolicy policy=policyService.selectById(gp.getPolicyId());
		List<ProductDto> productDtos = new ArrayList<>();
		List<OwnerWelfareDto> ownerWelfares = new ArrayList<>();
		for (CardLink cardLink : links) {
			if (cardLink.getProductId() != 0) {
				Product temp = productService.selectById(cardLink.getProductId());
				ProductDto productDto = new ProductDto();
				BeanUtils.copyProperties(productDto, temp);
				productDto.setCount(cardLink.getCount());
				productDto.setUsePeriod(cardLink.getUsePeriod());
				productDto.setLinkId(cardLink.getId());
				productDtos.add(productDto);
			} else {
				
				if (cardLink.getWelfareId() != null && cardLink.getWelfareId() != 0) {
					OwnerWelfareDto owDto=new OwnerWelfareDto();
					owDto.setLinkId(cardLink.getId());
					owDto.setWelfareId(cardLink.getWelfareId());
					ownerWelfares.add(owDto);
				}
			}
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("code", 2000);
		map.put("member", storeMember);
		map.put("policy", policy);
		map.put("productDtos",productDtos);
		map.put("ownerWelfares",ownerWelfares);
		return map;
	}
}
